<?php

/*============================================
Classe Userinstrumentconcert
============================================*/

class Userinstrumentconcert extends ModelObject{

	protected static $cached_object_list= array();
	protected $lft;
	protected $rght;
	protected $parent_id;
	protected $concert_id;
	protected $instrument;
	protected $user;
	protected $role;
	protected $nom;

	protected function __construct( $lft,$rght,$parent_id,$concert_id,$instrument,$user,$role,$nom){
		try{
			
			$this->lft = $lft;
			$this->rght = $rght;
			$this->parent_id = $parent_id;
			$this->concert_id = $concert_id;
			$this->instrument = $instrument;
			$this->user = $user;
			$this->role = $role;
			$this->nom = $nom;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getLft(){
		return $this->lft;
	}

	public function getRght(){
		return $this->rght;
	}

	public function getParentId(){
		return $this->parent_id;
	}

	public function getConcertId(){
		return Concert::get($this->concert_id);
	}

	public function getInstrument(){
		return Instrument::get($this->instrument);
	}

	public function getUser(){
		return User::get($this->user);
	}

	public function getRole(){
		return $this->role;
	}

	public function getNom(){
		return $this->nom;
	}

	public function setLft( $lft){
		try{
			$temp = $this->lft;
			if($this->lft !== $lft){
				$this->lft = $lft;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->lft = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setLft() a échouée",0,$e);
		}
	}

	public function setRght( $rght){
		try{
			$temp = $this->rght;
			if($this->rght !== $rght){
				$this->rght = $rght;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->rght = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setRght() a échouée",0,$e);
		}
	}

	public function setParentId( $parent_id){
		try{
			$temp = $this->parent_id;
			if($this->parent_id !== $parent_id){
				$this->parent_id = $parent_id;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->parent_id = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setParentId() a échouée",0,$e);
		}
	}

	public function setConcertId( $concert_id){
		try{
			$temp = $this->concert_id;
			if(is_null($concert_id)){
				throw new Exception("Le paramètre \$concert_id de type get_class($concert_id) est null alors que la colonne dans la base est marquée comme NOT NULL");
			}
			else if(!($concert_id instanceof Concert) || is_null($concert_id->getId())){
				throw new Exception("Le paramètre \$concert_id de type get_class($concert_id) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->concert_id !== $concert_id->getId()){
					$this->concert_id = $concert_id->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->concert_id = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setConcertId() a échouée",0,$e);
		}
	}

	public function setInstrument( $instrument){
		try{
			$temp = $this->instrument;
			if(is_null($instrument)){
				throw new Exception("Le paramètre \$instrument de type get_class($instrument) est null alors que la colonne dans la base est marquée comme NOT NULL");
			}
			else if(!($instrument instanceof Instrument) || is_null($instrument->getId())){
				throw new Exception("Le paramètre \$instrument de type get_class($instrument) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->instrument !== $instrument->getId()){
					$this->instrument = $instrument->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->instrument = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setInstrument() a échouée",0,$e);
		}
	}

	public function setUser( $user){
		try{
			$temp = $this->user;
			if(is_null($user)){
				throw new Exception("Le paramètre \$user de type get_class($user) est null alors que la colonne dans la base est marquée comme NOT NULL");
			}
			else if(!($user instanceof User) || is_null($user->getId())){
				throw new Exception("Le paramètre \$user de type get_class($user) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->user !== $user->getId()){
					$this->user = $user->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->user = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setUser() a échouée",0,$e);
		}
	}

	public function setRole( $role){
		try{
			$temp = $this->role;
			if($this->role !== $role){
				$this->role = $role;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->role = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setRole() a échouée",0,$e);
		}
	}

	public function setNom( $nom){
		try{
			$temp = $this->nom;
			if($this->nom !== $nom){
				$this->nom = $nom;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->nom = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setNom() a échouée",0,$e);
		}
	}

	public static function create( $lft,$rght,$parent_id,$concert_id,$instrument,$user,$role,$nom){
		try{
			$userinstrumentconcert = new Userinstrumentconcert($lft, $rght, $parent_id, $concert_id, $instrument, $user, $role, $nom);
			$userinstrumentconcert->insert();
			Userinstrumentconcert::cacheObject($userinstrumentconcert);
			return $userinstrumentconcert;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type Userinstrumentconcert a échouée",0,$e);
		}
	}

	public static function get( $userInstrumentConcert){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($userInstrumentConcert) || $userInstrumentConcert === "") {
				return null;
			}
			if(Userinstrumentconcert::existsInCache($userInstrumentConcert)) {
				return Userinstrumentconcert::getFromCache($userInstrumentConcert);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `userinstrumentconcert` WHERE `userInstrumentConcert` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$userInstrumentConcert));
			if ($row = $stmt->fetch()){
				$userinstrumentconcert = new Userinstrumentconcert( $row['lft'],$row['rght'],$row['parent_id'],$row['concert_id'],$row['instrument'],$row['user'],$row['role'],$row['nom']);
				$userinstrumentconcert->id =  $row['userInstrumentConcert'];
				$dbh->commit();
				Userinstrumentconcert::cacheObject($userinstrumentconcert);
				return $userinstrumentconcert;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $userInstrumentConcert . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `userinstrumentconcert` SET `lft` = :lft, `rght` = :rght, `parent_id` = :parent_id, `concert_id` = :concert_id, `instrument` = :instrument, `user` = :user, `role` = :role, `nom` = :nom WHERE `userInstrumentConcert` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':lft'=>$this->lft, ':rght'=>$this->rght, ':parent_id'=>$this->parent_id, ':concert_id'=>$this->concert_id, ':instrument'=>$this->instrument, ':user'=>$this->user, ':role'=>$this->role, ':nom'=>$this->nom, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `userinstrumentconcert` WHERE `userInstrumentConcert` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			Userinstrumentconcert::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `userinstrumentconcert` ( `lft`, `rght`, `parent_id`, `concert_id`, `instrument`, `user`, `role`, `nom`) VALUES (:lft, :rght, :parent_id, :concert_id, :instrument, :user, :role, :nom)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':lft'=>$this->lft, ':rght'=>$this->rght, ':parent_id'=>$this->parent_id, ':concert_id'=>$this->concert_id, ':instrument'=>$this->instrument, ':user'=>$this->user, ':role'=>$this->role, ':nom'=>$this->nom));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `userinstrumentconcert`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(Userinstrumentconcert::existsInCache($row['userInstrumentConcert'])){
					$obj = Userinstrumentconcert::getFromCache($row['userInstrumentConcert']);
				}
				else{
					$obj = new Userinstrumentconcert( $row['lft'],$row['rght'],$row['parent_id'],$row['concert_id'],$row['instrument'],$row['user'],$row['role'],$row['nom']);
				}
				if(!is_null($obj)){
					$obj->id = $row['userInstrumentConcert'];
					Userinstrumentconcert::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Userinstrumentconcert a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `userinstrumentconcert`';
			$sql_select = 'SELECT userInstrumentConcert FROM `userinstrumentconcert`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = Userinstrumentconcert::getObjectFromCache($row['userInstrumentConcert']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						Userinstrumentconcert::removeObjectFromCache($row['userInstrumentConcert']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type Userinstrumentconcert a échouée",0,$e);
		}
	}

	public static function getEnumValues( $enum_name,$table_name = "userinstrumentconcert"){
		return ModelObject::getEnumValues($enum_name,"userinstrumentconcert");
	}

	/*fonction de jointure*/
	public function getListConcertWhereracineUserInstrumentConcert( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$objs = array();
			$sql = 'SELECT `concert` FROM `concert`';
			$where = self::generateWhereClause($search_params);
			if($where === ""){
				$sql .= $where.' WHERE `racineUserInstrumentConcert` = :id';
			}
			else{
				$sql .= $where.' AND `racineUserInstrumentConcert` = :id';
			}
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$array[':id'] = $this->id;
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = Concert::get($row['concert']);
				if(!is_null($obj)){
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La recuperation de la liste d'objets de type Concert liés à l'objet de type Userinstrumentconcert ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

}